﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace CJJCH.Web.Manage
{
    public partial class StandardsList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                InitControl();
                InitRequest();
            }
        }

        protected void InitControl()
        {
            if (Request.QueryString["secondid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["secondid"]))
            {

                CJJCH.Model.T_NET_MenuSecond model = new Model.T_NET_MenuSecond();
                model = new CJJCH.BLL.T_NET_MenuSecond().GetModel(Convert.ToInt32(Request.QueryString["secondid"]));
                if (model != null)
                {
                    lblSecondId.Text = LTP.Common.StringClass.EncodeString(model.ID.ToString());
                    StandardsTop1.SecondId = Convert.ToInt32(lblSecondId.Text);
                    lblSecondName.Text = LTP.Common.StringClass.EncodeString(model.MenuName);
                    StandardsTop1.SecondName = LTP.Common.StringClass.EncodeString(lblSecondName.Text);
                    lblFirstID.Text = LTP.Common.StringClass.EncodeString(model.FirstID.ToString());
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack("操作错误！");
                }
                if (model.HaveChild == true)
                {
                    lblIS.Text = "True";
                    StandardsTop1.TypeID = 1;
                    IList<CJJCH.Model.T_NET_MenuThird> list = new List<CJJCH.Model.T_NET_MenuThird>();
                    list = (new CJJCH.BLL.T_NET_MenuThird()).GetDrop(Convert.ToInt32(lblSecondId.Text));
                    int typeCount = list.Count;
                    if (typeCount > 0)
                    {
                        for (int i = 0; i < typeCount; i++)
                        {
                            dropStandardsType.Items.Add(new ListItem(list[i].MenuName, list[i].ID.ToString()));
                        }
                        list = null;
                    }
                }
                else
                {
                    lblIS.Text = "False";
                    StandardsTop1.TypeID = 0;
                    type.Style.Add("display", "none");
                    TypeB.Style.Add("display", "none");

                }
            }
        }

        private void InitRequest()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["orderkey"]))
            {
                lblOrderKey.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["orderkey"].Trim());

            }
            if (!string.IsNullOrEmpty(Request.QueryString["ordertype"]))
            {
                lblOrderType.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["ordertype"].Trim());

            }
            if (!string.IsNullOrEmpty(Request.QueryString["keyword"]))
            {
                lblKeyword.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keyword"].Trim());
                txtKeyword.Text = lblKeyword.Text;
            }
            if (!string.IsNullOrEmpty(Request.QueryString["keytype"]) && LTP.Common.CheckInput.CheckDigit(Request.QueryString["keytype"].Trim()))
            {
                lblKeytype.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keytype"].Trim());
                dropStandardsType.SelectedValue = lblKeytype.Text;
            }
            if (!string.IsNullOrEmpty(Request.QueryString["betime"]) && LTP.Common.CheckInput.CheckDateTime(Request.QueryString["betime"].Trim()))
            {
                lblTimeBegin.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["betime"].Trim());
                txtBegin.Text = lblTimeBegin.Text;
            }
            if (!string.IsNullOrEmpty(Request.QueryString["edtime"]) && LTP.Common.CheckInput.CheckDateTime(Request.QueryString["edtime"].Trim()))
            {
                lblTimeOver.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["edtime"].Trim());
                txtEnd.Text = lblTimeOver.Text;
            }

            if (Request.QueryString["curpage"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["curpage"].Trim()))
            {
                int curPage = int.Parse(Request.QueryString["curpage"].ToString());
                AspNetPager1.RecordCount = curPage * AspNetPager1.PageSize;
                AspNetPager1.CurrentPageIndex = curPage;
            }
            else
            {
                AspNetPager1.CurrentPageIndex = 1;
            }

        }
        
        private void ReapterDataBinding()
        {
            string keyword = LTP.Common.StringClass.EncodeString(lblKeyword.Text.Trim());
            int keytype = 0;
            try
            {
                keytype = System.Convert.ToInt32(lblKeytype.Text.Trim());
            }
            catch { }
            string Timebegin = "";
            try
            {
                Timebegin = Convert.ToDateTime(lblTimeBegin.Text.Trim()).ToString();
            }
            catch { }
            string TimeOver = "";
            try
            {
                TimeOver = Convert.ToDateTime(lblTimeOver.Text.Trim()).AddDays(1).ToString();
            }
            catch { }
            string strOrder = "";
            if (lblOrderKey.Text.Trim() != "")
            {
                strOrder += lblOrderKey.Text.Trim();
                if (lblOrderType.Text.Trim() == "1")
                {
                    strOrder += " desc,";
                }
                else
                {
                    strOrder += " asc,";
                }
            }

            int totalCount = 0;
            IList<CJJCH.Model.T_NET_Standards> list = new List<CJJCH.Model.T_NET_Standards>();
            list = (new CJJCH.BLL.T_NET_Standards()).SelStandards(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, keyword, keytype, Timebegin, TimeOver, ref totalCount, strOrder, Convert.ToInt32(lblSecondId.Text));
            AspNetPager1.RecordCount = totalCount > (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize ? totalCount : AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize;

            this.rptStandards.DataSource = list;
            this.rptStandards.DataBind();

            if (list.Count == 0)
                litMessage.Text = "<table width=100%><tr style=height:18px bgcolor=#FFFFFF><td align='center'><font color=red>对不起，系统没有检索出有关信息！<font></td></tr></table>";
            else
                litMessage.Text = "";

            AspNetPager1.CustomInfoHTML = "共有：<font color=\"blue\"><b>" + totalCount.ToString() + "</b></font>条记录";
            AspNetPager1.CustomInfoHTML += " <font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
            AspNetPager1.CustomInfoHTML += "/<font color=\"blue\"><b>" + AspNetPager1.PageCount.ToString() + "</b></font>";
        }

        protected void rptStandards_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ((Label)e.Item.FindControl("lblDataTitel")).Text = ((CJJCH.Model.T_NET_Standards)e.Item.DataItem).DataTitle;
                ((HyperLink)e.Item.FindControl("lnkDataName")).Text = ((CJJCH.Model.T_NET_Standards)e.Item.DataItem).DataName;
                ((HyperLink)e.Item.FindControl("lnkDataName")).Attributes.Add("onclick", "ShowFinish('" + ((CJJCH.Model.T_NET_Standards)e.Item.DataItem).ID.ToString() + "'); return false;");

                if (((CJJCH.Model.T_NET_Standards)e.Item.DataItem).Menusecond.HaveChild == false)
                {
                    ((HtmlTableCell)e.Item.FindControl("tdType")).Style.Add("display", "none;");
                }
                else
                {
                    ((Label)e.Item.FindControl("lblType")).Text = ((CJJCH.Model.T_NET_Standards)e.Item.DataItem).Third.MenuName;
                }
                ((Label)e.Item.FindControl("lblDataAuthor")).Text = ((CJJCH.Model.T_NET_Standards)e.Item.DataItem).DataAuthor;
                ((Label)e.Item.FindControl("lblPlusDate")).Text = ((CJJCH.Model.T_NET_Standards)e.Item.DataItem).PlusDate.ToString("yyyy/MM/dd");
                ((LinkButton)e.Item.FindControl("lbtnEdit")).CommandArgument = ((CJJCH.Model.T_NET_Standards)e.Item.DataItem).ID.ToString();
                ((LinkButton)e.Item.FindControl("lbtnDel")).CommandArgument = ((CJJCH.Model.T_NET_Standards)e.Item.DataItem).ID.ToString();
                ((LinkButton)e.Item.FindControl("lbtnDel")).Attributes.Add("onclick", "javascript:if(confirm('确定要删除该数据吗？')){return true;}else{return false;}");

            }
        }

        protected void lbtnEdit_Click(object sender, EventArgs e)
        {
            if (LTP.Common.CheckInput.CheckGUID(((LinkButton)sender).CommandArgument))
            {
                Response.Redirect("StandardsEdit.aspx?curpage=" + AspNetPager1.CurrentPageIndex.ToString()
                    + "&keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                    + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                    + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                    + "&keytype=" + LTP.Common.StringClass.EncodeUrl(lblKeytype.Text.Trim())
                    + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblTimeBegin.Text.Trim())
                    + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblTimeOver.Text.Trim())
                    + "&secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim())
                    + "&firstid=" + LTP.Common.StringClass.EncodeUrl(lblFirstID.Text.Trim())
                    + "&secondname=" + LTP.Common.StringClass.EncodeUrl(lblSecondName.Text.Trim())
                    + "&ID=" + LTP.Common.StringClass.EncodeUrl(((LinkButton)sender).CommandArgument));

            }
            else
            {
                LTP.Common.WindowClass.WindowBack("操作错误！");
            }
        }

        protected void lbtbDel_Click(object sender, EventArgs e)
        {
            if (LTP.Common.CheckInput.CheckGUID(((LinkButton)sender).CommandArgument))
            {
                string error = "";
                error = (new CJJCH.BLL.T_NET_Standards()).DelStandards((new Guid(((LinkButton)sender).CommandArgument)));
                if (string.IsNullOrEmpty(error.Trim()))
                {
                    Response.Redirect("StandardsList.aspx?curpage=" + AspNetPager1.CurrentPageIndex.ToString()
                        + "&keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                        + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                        + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                        + "&keytype=" + LTP.Common.StringClass.EncodeUrl(lblKeytype.Text.Trim())
                        + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblTimeBegin.Text.Trim())
                        + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblTimeOver.Text.Trim())
                        + "&secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim())
                        + "&firstid=" + LTP.Common.StringClass.EncodeUrl(lblFirstID.Text.Trim())
                        + "&secondname=" + LTP.Common.StringClass.EncodeUrl(lblSecondName.Text.Trim()));
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack(error);
                }
            }
            else
                LTP.Common.WindowClass.WindowBack("操作错误！");
        }
        
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            ReapterDataBinding();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            lblKeyword.Text = LTP.Common.StringClass.EncodeString(txtKeyword.Text.Trim());
            lblTimeBegin.Text = LTP.Common.StringClass.EncodeString(txtBegin.Text.Trim());
            lblTimeOver.Text = LTP.Common.StringClass.EncodeString(txtEnd.Text.Trim());
            lblKeytype.Text = dropStandardsType.SelectedValue.Trim();
            AspNetPager1.CurrentPageIndex = 1;
        }

        #region 抬头排序功能区
        protected void lbtnDataTitel_Click(object sender, EventArgs e)
        {
            lblOrderKey.Text = "T_NET_Standards.DataTitle";
            if (lblOrderType.Text == "0")
            {
                lblOrderType.Text = "1";
            }
            else
            {
                lblOrderType.Text = "0";
            }
            AspNetPager1.CurrentPageIndex = 1;
        }

        protected void lbtnDataName_Click(object sender, EventArgs e)
        {
            lblOrderKey.Text = "T_NET_Standards.DataName";
            if (lblOrderType.Text == "0")
            {
                lblOrderType.Text = "1";
            }
            else
            {
                lblOrderType.Text = "0";
            }
            AspNetPager1.CurrentPageIndex = 1;
        }

        protected void lbtnThirdName_Click(object sender, EventArgs e)
        {
            lblOrderKey.Text = "T_NET_MenuThird.MenuName";
            if (lblOrderType.Text == "0")
            {
                lblOrderType.Text = "1";
            }
            else
            {
                lblOrderType.Text = "0";
            }
            AspNetPager1.CurrentPageIndex = 1;
        }

        protected void lbtnDataAuthor_Click(object sender, EventArgs e)
        {

            lblOrderKey.Text = "T_NET_Standards.DataAuthor";
            if (lblOrderType.Text == "0")
            {
                lblOrderType.Text = "1";
            }
            else
            {
                lblOrderType.Text = "0";
            }
            AspNetPager1.CurrentPageIndex = 1;
        }


        #endregion
    }
}